"""
Generates code lists from simplified JSON files.
Code lists will be stored in separate files, one item per line.
"""

__author__ = "Bart Hanssens <bart.hanssens@fedict.be>"


import argparse, os, logging
import io, json

logging.basicConfig(filename='codelist-datagov.log', level=logging.DEBUG)

def jsonparse(infile, codes):
	"""Parse JSON files and add to code lists."""
	logging.info('Parsing ' + str(infile))

	with open(infile, 'r') as f:
		data = json.load(f)

		for key in codes:
			if key in data:
				for value in data[key]:
					codes[key].add(value)


def writecodes(outdir, codes):
	"""Write code lists to separate output files."""
	for key in codes:
		logging.info('Writing ' + key)

		if codes[key]:
			name = os.path.join(outdir, key)
			with io.open(name, 'w', encoding='utf-8') as f:
				for c in sorted(codes[key]):
					if c:
						f.write(c + '\n')


def main():	
	parser = argparse.ArgumentParser(description='Extract codelists from JSON files')
	parser.add_argument('--indir', help='Input directory', required=True)
	parser.add_argument('--outdir', help='Output directory', required=True)
	
	args = parser.parse_args()
	
	# Init dict
	codes = dict(author = set(), license = set(), fmts = set(), freq = set(), 
				tags = set(), time = set(), geo = set(), org = set())

	for f in os.listdir(args.indir):
		jsonparse(os.path.join(args.indir, f), codes)

	writecodes(args.outdir, codes)
			
if __name__ == "__main__":
	main()
